package com.example.hszpad;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.hszpad.fragment.HSZControlFragment;
import com.example.hszpad.fragment.HSZViewFragment;
import com.example.hszpad.modbus.ModbusSocketClient;
import com.example.hszpad.util.ScreenUtil;
import com.intelligt.modbus.jlibmodbus.Modbus;
import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
import com.intelligt.modbus.jlibmodbus.master.ModbusMaster;
import com.intelligt.modbus.jlibmodbus.master.ModbusMasterFactory;
import com.intelligt.modbus.jlibmodbus.tcp.TcpParameters;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ModbusSocketClient.OnReceiveListener {
    private static final int action_control = 1;
    private static final int action_view = 2;
    private int curAction;
    private ToggleButton btn_change;
    private HSZControlFragment hszControlFragment = new HSZControlFragment();
    private HSZViewFragment hszViewFragment = new HSZViewFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ScreenUtil.hideBottomNav(this);
        btn_change = findViewById(R.id.btn_change);
        btn_change.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                FragmentManager fm = MainActivity.this.getSupportFragmentManager();
                FragmentTransaction tran = fm.beginTransaction();
                if (isChecked) {
                    showAction(action_view);
                } else {
                    showAction(action_control);
                }
                tran.commit();
            }
        });
        showAction(action_view);
    }

    private void showAction(int action) {
        if (action == curAction) {
            return;
        }
        curAction = action;
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction tran = fm.beginTransaction();
        if (action == action_view) {
            tran.replace(R.id.fl_content, hszViewFragment);
        } else {
            tran.replace(R.id.fl_content, hszControlFragment);
        }
        tran.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_conn:
                //获取地址信息
//                ip = et_ip.getText().toString();
//                port = Integer.parseInt(et_port.getText().toString());
//                address = Integer.parseInt(et_address.getText().toString());
//                quality = Integer.parseInt(et_quality.getText().toString());
//
//                new Thread() {
//                    @Override
//                    public void run() {
//                        // 设置主机TCP参数
//                        TcpParameters tcpParameters = new TcpParameters();
//
//                        // 设置TCP的ip地址
//                        InetAddress adress = null;
//                        try {
//                            adress = InetAddress.getByName(ip);
//                        } catch (UnknownHostException e) {
//                            e.printStackTrace();
//                        }
//
//                        // TCP参数设置ip地址
//                        // tcpParameters.setHost(InetAddress.getLocalHost());
//                        tcpParameters.setHost(adress);
//
//                        // TCP设置长连接
//                        tcpParameters.setKeepAlive(true);
//                        // TCP设置端口，这里设置是默认端口502
//                        tcpParameters.setPort(port);
//                        if (modbusSocketClient == null) {
//                            modbusSocketClient = new ModbusSocketClient(MainActivity.this);
//                            modbusSocketClient.setmTcpParameters(tcpParameters);
//                        }
//                        modbusSocketClient.setOnReceiveListener(MainActivity.this);
//                        modbusSocketClient.conn();
//                    }
//                }.start();
                break;
        }
    }


    @Override
    public void receive(int[] registers) {

//        myAdapter.refresh(registers);
    }

    @Override
    public void catchException(Exception e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
